/**
 * grayrabbit Inc.
 * Copyright (c) 2011-2012 All Rights Reserved.
 */
package com.grayrabbit.slacker.dababase;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.HashSet;
import java.util.Set;

/**
 * 数据库连接类
 * 
 * @author liyd
 * @version $Id: DatabaseProvider.java, v 0.1 2012-5-8 下午1:43:27 liyd Exp $
 */
public abstract class AbstractDatabaseProvider {

    /** 驱动类 */
    protected String     driverClassName;

    /** 连接字符串 */
    protected String     jdbcUrl;

    /** 用户名 */
    protected String     username;

    /** 密码 */
    protected String     password;

    /** 数据库连接对象 */
    protected Connection connection;

    private Set<String>  dateType;

    /**
     * 构造方法
     */
    public AbstractDatabaseProvider() {

        dateType = new HashSet<String>();
        dateType.add("java.sql.Date");
        dateType.add("java.sql.Timestamp");

    }

    /**
     * 是否是sql的date类型
     * 
     * @param className
     * @return
     */
    protected boolean isSqlDateType(String className) {
        return dateType.contains(className);
    }

    /**
     * 创建数据库连接
     * 
     * @throws Exception
     */
    public void createConnection() {

        try {

            Class.forName(this.driverClassName);
            connection = DriverManager.getConnection(this.jdbcUrl, this.username, this.password);

        } catch (Exception e) {
            throw new RuntimeException("创建数据库连接失败");
        }
    }

    /**
     * 关闭数据库连接
     */
    public void closeConnection() {
        try {
            connection.close();
        } catch (SQLException e) {
            throw new RuntimeException("关闭数据库连接对象失败");
        }
    }

}
